let data = {
    "1":{
        "id":1,
        "preNodeId":0,
        "nextNodeId":0,
        "text":"开始",
        "obj":null,
        "x":50,
        "y":50,
        "width":100,
        "height":50,
        "link":null
    },
    "2":{
        "id":2,
        "preNodeId":1,
        "nextNodeId":0,
        "text":"结束",
        "obj":null,
        "x":50,
        "y":250,
        "width":100,
        "height":50,
        "link":null
    },
    "3":{
        "id":3,
        "preNodeId":1,
        "nextNodeId":0,
        "text":"中间",
        "obj":null,
        "x":150,
        "y":150,
        "width":100,
        "height":50,
        "link":null
    },
    "4":{
        "id":4,
        "preNodeId":3,
        "nextNodeId":0,
        "text":"中间1",
        "obj":null,
        "x":250,
        "y":250,
        "width":100,
        "height":50,
        "link":null
    }
};

let graph = new joint.dia.Graph;

let paper = new joint.dia.Paper({
    el:$("#paper"),
    width:1000,
    height:600,
    gridSize:10,
    model:graph
});

let addCells = [];
for (let i in data){
    if(data[i]['obj'] == null){
        //修改id
        data[i]['obj'] = new joint.shapes.standard.Rectangle({id:data[i]['id']});
        data[i]['obj'].position(data[i]['x'],data[i]['y']);
        data[i]['obj'].resize(data[i]['width'],data[i]['height']);
        data[i]['obj'].attr("label/text",data[i]['text']);
        console.log(data[i]['obj'])
    }

    addCells.push(data[i]['obj'])

    if(data[i]["nextNodeId"] != 0){
        data[i]['link'] = new joint.shapes.standard.Link();
        data[data[i]["nextNodeId"]]['obj'] = new joint.shapes.standard.Rectangle({id:data[data[i]["nextNodeId"]]['id']});
        data[data[i]["nextNodeId"]]['obj'].position(data[data[i]["nextNodeId"]]['x'],data[data[i]["nextNodeId"]]['y']);
        data[data[i]["nextNodeId"]]['obj'].resize(data[data[i]["nextNodeId"]]['width'],data[data[i]["nextNodeId"]]['height']);
        data[data[i]["nextNodeId"]]['obj'].attr("label/text",data[data[i]["nextNodeId"]]['text']);

        data[i]['link'].source(data[i]['obj']);
        data[i]['link'].target(data[data[i]["nextNodeId"]]['obj']);

        addCells.push(data[i]['link']);
    }

    if(data[i]["preNodeId"] != 0){
        data[i]['link'] = new joint.shapes.standard.Link();

        data[i]['link'].source(data[data[i]["preNodeId"]]['obj']);
        data[i]['link'].target(data[i]['obj']);

        addCells.push(data[i]['link']);
    }
}


graph.addCells(addCells)